package com.slxy.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Text {
    private static void a(){
        // 1. 加载xml文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 2. 获取到对象
        // 得到一个名称为a的对象 byName方式
        DemoA a1 = (DemoA) context.getBean("a1");
        // 得到一个类型是DemoA的对象 byType方式
//        DemoA a2 = context.getBean(DemoA.class);
        //根据名称和类型产生获取对象byName+byType方式
        DemoA a3 = context.getBean("a2",DemoA.class);
        //调用此对象及方法
           a1.a();
//        a2.a();
        a3.a();
    }
    // 探讨给对象属性赋值
    private static void b(){
        // 1. 加载xml文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoB b=context.getBean(DemoB.class);
        DemoC c=context.getBean(DemoC.class);
        DemoD d=context.getBean(DemoD.class);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);

    }
    private static void c(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoE e1= context.getBean("e1",DemoE.class);
        System.out.println(e1);
        DemoE e2= context.getBean("e2",DemoE.class);
        System.out.println(e2);
        DemoE e3= context.getBean("e3",DemoE.class);
        System.out.println(e3);
    }
    private static void d(){
//        默认为勤加载
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//        懒加载必须getbean
        DemoF f= context.getBean("f",DemoF.class);
    }
     private static void e(){
        //默认为单例模式
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoG g1= context.getBean("g",DemoG.class);
        DemoG g2= context.getBean("g",DemoG.class);
         System.out.println(g1);
         System.out.println(g2);
         System.out.println(g1==g2);
     }
    public static void main(String[] args) {
    e();
    }
}
